﻿using System;
using System.Collections.Generic;
using Fetches.Fetch.Utils;

namespace Fetches.Fetch.Dto
{
    /// <summary>
    /// Page是数据流里最重要的对象，所有解析针对Page
    /// </summary>
    public class ResponsePage
    {
        /// <summary>
        /// Page的Id用于持久化时作为辅助信息
        /// 格式为timestamp+MD5(Url),timestamp在前面便于排序,对Url计算hash使id保持格式一致性
        /// </summary>
        public string Id { get; set; }
        public Request Request { get; set; }

        #region Http返回信息

        public int StatusCode { get; set; }
        /// <summary>
        /// 原始的页面代码
        /// </summary>
        public string SourceCode { get; set; }
        /// <summary>
        /// 资源本地化后的页面代码
        /// </summary>
        public string LocalizedSourceCode { get; set; }

        /// <summary>
        /// Headers
        /// </summary>
        public Dictionary<string, HashSet<string>> Headers { get; set; } = new Dictionary<string, HashSet<string>>();

        #endregion

        /// <summary>
        /// 下载消耗的时间
        /// </summary>
        public int ElapsedMilliseconds { get; set; }
        #region 存储辅助信息
        /// <summary>
        /// 使用时间戳来完成对同一Url对应结果的多次存储
        /// </summary>
        public long TimeStamp { get; set; }
        /// <summary>
        /// 内容Hash,用于内容存储时降低存储压力
        /// </summary>
        public string ContentHash { get; set; }
        #endregion

        /// <summary>
        /// 填充需要计算的值
        /// </summary>
        public void Calc()
        {
            ContentHash = HashHelper.Sha1(SourceCode);
            TimeStamp = DateTime.Now.ToSeconds();
            Id = TimeStamp + HashHelper.Md5(Request.Url.OriginalString);
        }
    }
}